package com.badshun.player;

import com.badlogic.gdx.math.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badshun.box2d.*;
import com.badlogic.gdx.physics.box2d.*;
import com.badshun.stage.*;
import com.badshun.skill.*;

public class MainPlayer {
	public Vector2 position;
	private float width;
	private float height;
	
	private Animation leftAnimation;
	private Animation rightAnimation;
	private Animation upAnimation;
	private Animation downAnimation;
	
	private TextureRegion keyFrame;
	
	public ACTION action;
	public DIRECTION direction;
	public IDLE idle;
	
	public boolean isHitNPC;
	public boolean isHitDoor;
	public boolean isHitEnemy;
	
	private OrthographicCamera camera;
	
	private SpriteBatch batch;
	
	private float time;
	
	private Box2DWorld world;
	public Body playerBody;
	
	public Texture headImage;
	
	public String name;
	
	public EnvironmentStageManager esm;
	
	public String hiter;
	
	public String enemyName = "";
	
	public Skill skill;
	public Skill bigFire;
	public Skill atk;
	
	public MainPlayer(EnvironmentStageManager esm,OrthographicCamera camera,Box2DWorld world) {
		this.esm = esm;
		this.camera = camera;
		this.world = world;
		
		headImage = new Texture(Gdx.files.internal("head/mainPlayerHead.jpg"));
		
		name = "李狗蛋";
		
		batch = new SpriteBatch();
		
		definePlayer();
	}
	
	private void definePlayer() {
		action = ACTION.IDLE;
		direction = DIRECTION.UP;
		idle = IDLE.IDLE_UP;
		
		position = new Vector2((Gdx.graphics.getWidth() / 2 + width / 2) / 2,(Gdx.graphics.getHeight() / 2 + height / 2) / 2);
		
		Texture texture = new Texture(Gdx.files.internal("player/main_player.jpg"));

		width = texture.getWidth() / 4;
		height = texture.getHeight() / 4;
		
		BodyDef bdef = new BodyDef();
		bdef.position.set(position.x,position.y);
		bdef.type = BodyDef.BodyType.DynamicBody;
		
		playerBody = world.world.createBody(bdef);
		
		PolygonShape shape = new PolygonShape();
		shape.setAsBox(width / 2,height / 2);
		
		FixtureDef fdef = new FixtureDef();
		fdef.shape = shape;
		fdef.filter.categoryBits = 2;
		fdef.filter.maskBits = 4 | 8 | 16;
		
		playerBody.createFixture(fdef).setUserData("PLAYER");
		
		shape.setAsBox(width / 2 + 2, height / 2 + 2);
		
		fdef.shape = shape;
		fdef.filter.categoryBits = 2;
		fdef.filter.maskBits = 4 | 8 | 16;
		fdef.isSensor = true;
		
		playerBody.createFixture(fdef).setUserData("PLAYER");
		
		TextureRegion[][] playerTextureRegionList = TextureRegion.split(texture,texture.getWidth() / 4,texture.getHeight() / 4);
		
		leftAnimation = new Animation(0.08f,playerTextureRegionList[1]);
		rightAnimation = new Animation(0.08f,playerTextureRegionList[2]);
		upAnimation = new Animation(0.08f,playerTextureRegionList[3]);
		downAnimation = new Animation(0.08f,playerTextureRegionList[0]);
		
		leftAnimation.setPlayMode(Animation.PlayMode.LOOP);
		rightAnimation.setPlayMode(Animation.PlayMode.LOOP);
		upAnimation.setPlayMode(Animation.PlayMode.LOOP);
		downAnimation.setPlayMode(Animation.PlayMode.LOOP);
	}
	
	public void resetPlayer(Vector2 position,DIRECTION direction) {
		world.world.destroyBody(playerBody);
		
		BodyDef bdef = new BodyDef();
		bdef.position.set(position.x,position.y);
		bdef.type = BodyDef.BodyType.DynamicBody;

		playerBody = world.world.createBody(bdef);

		PolygonShape shape = new PolygonShape();
		shape.setAsBox(width / 2,height / 2);

		FixtureDef fdef = new FixtureDef();
		fdef.shape = shape;
		fdef.filter.categoryBits = 2;
		fdef.filter.maskBits = 4 | 8 | 16;

		playerBody.createFixture(fdef).setUserData("PLAYER");

		shape.setAsBox(width / 2 + 2, height / 2 + 2);

		fdef.shape = shape;
		fdef.filter.categoryBits = 2;
		fdef.filter.maskBits = 4 | 8 | 16;
		fdef.isSensor = true;

		playerBody.createFixture(fdef).setUserData("PLAYER");
	}
	
	public void createSkill_BigFire(MainPlayer player) {
		bigFire = new Skill("bigFire",7,player,world,camera);
		
		skill = bigFire;
		
		bigFire = null;
	}
	
	public void createSkill_ATK(MainPlayer player) {
		atk = new Skill("atk",6,player,world,camera);

		skill = atk;

		atk = null;
	}
	
	private void checkDirection() {
		if(playerBody.getLinearVelocity().x == 0 && playerBody.getLinearVelocity().y == 0) action = ACTION.IDLE;
		if(playerBody.getLinearVelocity().x < 0 && playerBody.getLinearVelocity().y == 0) direction = DIRECTION.LEFT;
		if(playerBody.getLinearVelocity().x > 0 && playerBody.getLinearVelocity().y == 0f) direction = DIRECTION.RIGHT;
		if(playerBody.getLinearVelocity().x == 0 && playerBody.getLinearVelocity().y > 0) direction = DIRECTION.UP;
		if(playerBody.getLinearVelocity().x == 0 && playerBody.getLinearVelocity().y < 0) direction = DIRECTION.DOWN;
	}
	
	public TextureRegion getKeyFrame(float time) {
		if(direction == DIRECTION.LEFT && action == ACTION.MOVE) return leftAnimation.getKeyFrame(time);
		else if(direction == DIRECTION.RIGHT && action == ACTION.MOVE) return rightAnimation.getKeyFrame(time);
		else if(direction == DIRECTION.UP && action == ACTION.MOVE) return upAnimation.getKeyFrame(time);
		else if(direction == DIRECTION.DOWN && action == ACTION.MOVE) return downAnimation.getKeyFrame(time);
		else if(action == ACTION.IDLE) {
			if(idle == IDLE.IDLE_LEFT) return leftAnimation.getKeyFrames()[0];
			else if(idle == IDLE.IDLE_RIGHT) return rightAnimation.getKeyFrames()[0];
			else if(idle == IDLE.IDLE_UP) return upAnimation.getKeyFrames()[0];
			else if(idle == IDLE.IDLE_DOWN) return downAnimation.getKeyFrames()[0];
		}
		
		return null;
	}
	
	public void render() {
		position.set(playerBody.getPosition().x - width / 2,playerBody.getPosition().y - height / 2);
		
		checkDirection();
		
		batch.setProjectionMatrix(camera.combined);
		batch.begin();
		
		keyFrame = getKeyFrame(time);
		batch.draw(keyFrame,position.x,position.y,width,height);
		
		batch.end();
		
		time += Gdx.graphics.getDeltaTime();
		
		if(skill != null) {
			skill.play();
			
			if(skill.stop) {
				skill.destroy();
				skill = null;
			}
		}
	}
	
	public enum ACTION {
		ATK,MOVE,TALK,IDLE
	}
	
	public enum DIRECTION {
		LEFT,RIGHT,UP,DOWN
	}
	
	public enum IDLE {
		IDLE_LEFT,IDLE_RIGHT,IDLE_UP,IDLE_DOWN
	}
}
